import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import {message} from 'antd'
import { convertContent } from '../constant/lang-util'

export const refreshAllTopicTask = allTopicTasks => ({
  type: 'REFRESH_ALL_TOPIC_TASKS',
  allTopicTasks
})
export const refreshProgramsAndSubTopics = programsAndSubTopics => ({
  type: 'REFRESH_PROGRAMS_AND_SUB_TOPICS',
  programsAndSubTopics
})

export const getAllTopicTask = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}/topicTask`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshAllTopicTask(res.body))
      }
    })()
  }
}

export const deleteTopic = (programId, topicId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/topics/${topicId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}

export const addTopic = (programId, newTopic) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/topics`, newTopic)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}

export const updateTopic = (programId, topicId, value) => {
  const data = {'title': value}
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/topics/${topicId}`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}
export const changeTopicVisible = (visibility, id, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/topics/${id}/visibility`, {visibility})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}

export const getProgramsAndSubTopics = () => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/subTopics`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshProgramsAndSubTopics(res.body))
      }
    })()
  }
}
export const copyToTopic = (organizationId, sourceTaskIds, topicId, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/tasks/organizations/${organizationId}/duplication`, {sourceTaskIds, topicId})
      if (res.status === HTTP_CODE.CREATED) {
        message.info(convertContent('复制成功'))
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}
export const moveToTopic = (sourceTaskIds, topicId, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/tasks/transfer`, {sourceTaskIds, topicId})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.info(convertContent('移动成功'))
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}

export const dragSort = (data, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/topics`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAllTopicTask(programId))
      }
    })()
  }
}
